﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GsOrganization.Common.Extend
{

    /// <summary>
    /// 时间扩展类
    /// </summary>
    public static class DateTimeExtend
    {
        /// <summary>
        /// 起始时间
        /// </summary>
        private static DateTime dt_start = new DateTime(1970, 1, 1);

        /// <summary>
        /// 获取当前时间戳（秒）
        /// </summary>
        /// <returns></returns>
        public static int GetTimeStampS() => ((int)(DateTime.Now - dt_start).TotalSeconds);

        /// <summary>
        /// 获取指定时刻时间戳，秒级
        /// </summary>
        /// <returns></returns>
        public static int GetTimeStampS(this DateTime dateTime) => ((int)(dateTime - dt_start).TotalSeconds);


        /// <summary>
        /// 获取当前时刻时间戳,毫秒级
        /// </summary>
        /// <returns></returns>
        public static long GetTimeStampMS() => ((long)(DateTime.Now - dt_start).TotalMilliseconds);

        /// <summary>
        /// 获取指定时刻时间戳,毫秒级
        /// </summary>
        /// <returns></returns>
        public static long GetTimeStampMS(this DateTime dateTime) => ((long)(dateTime - dt_start).TotalMilliseconds);

        /// <summary>
        /// 时间戳转时间,秒级
        /// </summary>
        /// <param name="timestamp"></param>
        /// <returns></returns>
        public static DateTime FromTimeStampS(this int timestamp) => dt_start.AddSeconds(timestamp);

        /// <summary>
        /// 时间戳转时间，毫秒级
        /// </summary>
        /// <param name="timestamp"></param>
        /// <returns></returns>
        public static DateTime FromTimeStampMS(long timestamp) => dt_start.AddMilliseconds(timestamp);
    }
}
